Extends Keyword

Used in a method declaration to indicate that the method is to be called using the object's syntax, i.e., as a method belonging to the object.

Extends parameter As Object

PartTypeDescription
parameter The name of the first parameter.
Object Any valid Object type The Object type from which the new method is to be called.


Notes

The Extends keyword enables you to call a user-defined method as a method belonging to a class. You use the Extends keyword only for the first parameter in the method declaration. Extends indicates that this parameter is to be used on the left side of the dot operator ("."). The remaining parameters in the declaration, if any, are normal parameters.

It is possible to set a default value for a parameter in a call that uses Extends as long as the default value is not set for the Extends parameter itself.

Methods declared in this way are sometimes called "class extension methods." You can use Extends only for methods in a module.

Extends cannot be used to override another method, merely to add methods to the class. Extension methods are not virtual since they are not actually part of the class.


Example

This example adds a method called SelectAll to the EditField class. The new method selects all the text in the EditField.

In a module, create the method with the declaration:

SelectAll (Extends e as EditField)

Although e appears to be a parameter that is passed to the method, it isn't. Since the Extends keyword is used, it merely indicates that the method will be added to the EditField class. When you call the SelectAll method, you don't pass any parameters.

The code for the SelectAll method is:

e.SelStart = 0
e.SelLength= Len(e.Text)

To call the new method, simply use it as if it was built into the REALbasic language. For example, you could add a Select All menu item to the Edit menu and then use the following line as its menu handler:

EditField1.SelectAll

This assumes that the window has an EditField named EditField1.

To use the SelectAll method in other applications, simply copy the module that contains it to another project.


See Also

Extends can only be used on the first parameter Error.